import {Table, TableProps} from "antd";
import {useMemo} from "react";
import {TableList, TablePage} from "@/common/httptypes";
import {hasEmpower} from "@/config/route/route-func";
import {ColumnGroupType, ColumnType} from "antd/es/table/interface";
import {Dispatch} from "redux";


export type  HTableEmpowerType = { hide?: boolean, empower?: string }
export type HTableColumns<RecordType> = ((ColumnGroupType<RecordType> | ColumnType<RecordType>) & HTableEmpowerType)[]
export type HTableProps<RecordType> = {
    rowKey: string;
    unSort?: boolean;
    scroll?: {
        y: number | string,
        x: number | string,
    };
    dispatch?: Dispatch;
    columns: HTableColumns<RecordType>
    tablePageData?: TablePage<RecordType> | TableList<RecordType>
} & TableProps<any>


export default function HTable<RecordType>(props: HTableProps<RecordType>) {
    const {columns, dispatch, tablePageData, unSort, rowKey, ...other} = props
    const result = useMemo(() => {
            const result: ((ColumnGroupType<RecordType> | ColumnType<RecordType>) & HTableEmpowerType)[] = [];
            if (!unSort) {
                result.push({title: "序号", dataIndex: 'sortIndex', width: '50px', fixed: 'left'})
            }
            for (let i in columns) {
                const column = columns[i];
                const {hide, empower} = column;
                if (hide) {
                    continue
                }
                if (hasEmpower(empower)) {
                    column['ellipsis'] = {showTitle: true};
                    result.push(column);
                }
            }
            return result;
        }
        , [columns, unSort])
    return <>
        <Table
            {...other}
            loading={props.loading}
            style={{paddingBottom: "20px", marginTop: "5px"}}
            columns={result}
            pagination={false}
            scroll={{
                y: '52vh',
                x: '1100px'
            }}
            size={"middle"}
            rowKey={rowKey}
            dataSource={tablePageData?.records?.map((data, index) => {
                return {...data, sortIndex: index + 1}
            })}

        />
    </>

}
